package com.tzinfo.tzt.activity;

import android.annotation.SuppressLint;
import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TabHost;

import com.tzinfo.tzt.R;
import com.tzinfo.tzt.activity.util.MyApplication;
import com.tzinfo.tztbase.TztBaseMainActivity;
import com.umeng.update.UmengUpdateAgent;

@SuppressLint("NewApi")
public class MainActivity extends TabActivity {
	private TabHost tabHost;
	private RadioGroup radioGroup;
	private MyApplication mApp;

	@Override
	protected void onStart() {
		// TODO Auto-generated method stub
		super.onStart();
		if (mApp.isExit()) {
			finish();
		}
	}

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		mApp = (MyApplication) getApplication();
		mApp.setExit(false);

		tabHost = getTabHost();
		tabHost.addTab(tabHost.newTabSpec("second_hand")
				.setIndicator("SECOND_HOUSE")
				.setContent(new Intent(this, OldHouseActivity.class)));
		tabHost.addTab(tabHost.newTabSpec("new_house")
				.setIndicator("NEW_HOUSE")
				.setContent(new Intent(this, NewHouseActivity.class)));
		tabHost.addTab(tabHost.newTabSpec("rent_house")
				.setIndicator("RENT_HOUSE")
				.setContent(new Intent(this, RentHouseActivity.class)));
		tabHost.addTab(tabHost.newTabSpec("list_house")
				.setIndicator("LIST_HOUSE")
				.setContent(new Intent(this, SeeNoteListActivity.class)));
		tabHost.addTab(tabHost
				.newTabSpec("my_page")
				.setIndicator("MY_PAGE")
				.setContent(
						new Intent(this, MyPageActivity.class)
								.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)));

		radioGroup = (RadioGroup) findViewById(R.id.radiogroup);

		radioGroup.setOnCheckedChangeListener(checkedChangeListener);

		Intent intent = getIntent();
		int no = intent.getIntExtra("TabNo", 0);
		switch (no) {
		case 2:
			tabHost.setCurrentTabByTag("new_house");
			((RadioButton) (radioGroup.getChildAt(1))).setChecked(true);
			break;
		case 3:
			tabHost.setCurrentTabByTag("rent_house");
			((RadioButton) (radioGroup.getChildAt(2))).setChecked(true);
			break;
		case 4:
			tabHost.setCurrentTabByTag("list_house");
			((RadioButton) (radioGroup.getChildAt(3))).setChecked(true);
			break;
		case 5:
			tabHost.setCurrentTabByTag("my_page");
			((RadioButton) (radioGroup.getChildAt(4))).setChecked(true);
			break;
		default:
			tabHost.setCurrentTabByTag("second_hand");
			((RadioButton) (radioGroup.getChildAt(0))).setChecked(true);
			break;
		}

		UmengUpdateAgent.setUpdateOnlyWifi(false);
		UmengUpdateAgent.update(this);
		UmengUpdateAgent.setUpdateAutoPopup(true);

	}

	private OnCheckedChangeListener checkedChangeListener = new OnCheckedChangeListener() {
		public void onCheckedChanged(RadioGroup group, int checkedId) {
			switch (checkedId) {
			case R.id.radio_btn1:
				tabHost.setCurrentTabByTag("second_hand");
				break;
			case R.id.radio_btn2:
				tabHost.setCurrentTabByTag("new_house");
				break;
			case R.id.radio_btn3:
				tabHost.setCurrentTabByTag("rent_house");
				break;
			case R.id.radio_btn4:
				tabHost.setCurrentTabByTag("list_house");
				break;
			case R.id.radio_btn5:
				tabHost.setCurrentTabByTag("my_page");
				break;
			}
		}
	};
	public boolean dispatchKeyEvent(KeyEvent event) {
		if (event.getAction() == KeyEvent.ACTION_DOWN
				&& event.getKeyCode() == KeyEvent.KEYCODE_BACK
				&& event.getRepeatCount() == 0) {
			Intent intent=new Intent(this, TztBaseMainActivity.class);
			intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
			startActivity(intent);
			finish();
			return false;
		} else {
			return super.dispatchKeyEvent(event);
		}
	}
	
	@Override
	public void finish() {
		if(MapRoutePlanActivity.activity!=null){
			MapRoutePlanActivity.activity.finish();
		}
		if(NewHouseMapActivity.activity!=null){
			NewHouseMapActivity.activity.finish();
		}
		if(OldHouseMapActivity.activity!=null){
			OldHouseMapActivity.activity.finish();
		}
		if(RentHouseMapActivity.activity!=null){
			RentHouseMapActivity.activity.finish();
		}
		
		super.finish();
	}

}